import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

class PopupInput extends StatelessWidget {
  final String title;
  final double width;
  final double height;
  final double? dialogHeight;
  final double dialogMaxHeight;
  final String select;
  List<String> datas;
  final ValueChanged<String>? onChanged;
  final AsyncValueGetter<List<String>?>? onClicked;

  PopupInput({
    super.key,
    this.title = "选择",
    required this.select,
    List<String>? datas,
    this.width = 150,
    this.height = 40,
    this.dialogHeight,
    this.dialogMaxHeight = 240,
    this.onChanged,
    this.onClicked,
  }) : datas = datas ?? const [];

  @override
  Widget build(BuildContext context) {
    return _inputWidget();
  }

  //输入框
  Widget _inputWidget() {
    return Container(
      width: width,
      height: height,
      margin: const EdgeInsets.only(left: 10),
      padding: const EdgeInsets.all(0),
      child: Builder(builder: (context) {
        return InkWell(
            onTap: () async {
              final result = await onClicked?.call();
              if (result != null) {
                datas = result;
                WidgetsBinding.instance.addPostFrameCallback((_) {
                  _dialog(context);
                });
              }
            },
            borderRadius: BorderRadius.circular(10),
            child: InputDecorator(
              decoration: InputDecoration(
                  labelText: title,
                  contentPadding: EdgeInsets.zero,
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(10.0),
                  )),
              child: Center(
                child: Text(select),
              ),
            ));
      }),
    );
  }

  //模仿DropdownButton
  void _dialog(BuildContext context) {
    SmartDialog.showAttach(
      targetContext: context,
      usePenetrate: true,
      nonAnimationTypes: [SmartNonAnimationType.maskClose_nonAnimation],
      builder: (_) {
        return Container(
          width: width,
          height: dialogHeight,
          constraints: BoxConstraints(maxHeight: dialogMaxHeight, minHeight: 40),
          margin: const EdgeInsets.all(10),
          decoration: BoxDecoration(
            color: Colors.white,
            boxShadow: const [BoxShadow(color: Colors.black12, blurRadius: 8, spreadRadius: 0.2)],
            borderRadius: BorderRadius.circular(5),
          ),
          child: datas.isNotEmpty ? _buildContent(context) : _buildEmpty(context),
        );
      },
    );
  }

  Widget _buildContent(BuildContext context) {
    return ListView(
      shrinkWrap: true,
      children: List.generate(datas.length, (index) {
        final item = datas[index];
        return Material(
          color: Colors.white,
          child: InkWell(
            onTap: () {
              SmartDialog.dismiss();
              onChanged?.call(item);
            },
            child: Container(
              width: width,
              height: height,
              alignment: Alignment.center,
              child: Text(item),
            ),
          ),
        );
      }),
    );
  }

  Widget _buildEmpty(BuildContext context) {
    return SizedBox(
      width: width,
      height: height,
      child: const Center(child: Text("暂无数据")),
    );
  }
}
